<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/8/11
 * Time: 10:54
 */
namespace Admin\Controller;

use Admin\Api\DatatreeApi;
use Admin\Api\ViewPageApi;
use Admin\Api\ViewPageItemApi;
use Shop\Api\CategoryApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;

abstract class ViewPageController extends AdminController{

    protected function xiao_display(){
        $this->theme('20160715')->display();
    }

    /**
     * 生成2级类目json
     */
    public function queryCategory(){

        if(IS_GET){
            $ret = [];

            $result = apiCall(CategoryApi::QUERY_NO_PAGING,[]);

            if(!$result['status']){
                $this->ajaxReturn($ret);
            }

            $info = $result['info'];

            $parent_group = [];
            //按照父级ID分组结果
            foreach($info as $val){
                $parent_group[$val['parent']][] = $val;
            }
            $ret = $this->getCategroyChild(0,$info);

            $this->ajaxReturn($ret);
        }

    }

    /**
     * 产销国数据
     */
    public function queryOriginCountry(){

        if(IS_GET){
            $ret = [];
            $map = [
                'parentid' => 35 //国别代码
            ];
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,[$map,false,'id,name,code']);
            if($result['status']){
                if(!is_null($result['info'])){
                    $ret = $result['info'];
                }
            }
            $this->ajaxReturn($ret);
        }

    }

    private function getCategroyChild($parentid,$array=[]){
        $ret = [];
        foreach($array as $val){
            if($val['parent']==$parentid){
                $child = $this->getCategroyChild($val['id'],$array);
                $temp = [
                    'value' => $val['id'],
                    'label' => $val['name']
                ];
                if($child) $temp['children'] = $child;
                $ret[] = $temp;
            }
        }
        return empty($ret) ? false : $ret;

    }

    /**
     * 查找商品json
     */
    public function queryProduct(){

        if(IS_POST){

            $map = ['uid'=>UID];
            $store_id = 0;
            $result = apiCall("Shop/Store/getInfo",array($map));
            if($result['status']){
                $store_id = $result['info']['id'];
            }
            if(empty($store_id)){
                $this->ajaxReturn(['status'=>false,'info'=>'请先创建店铺!']);
            }

            $cate_id = I('post.cate_id');
            $curpage = I('post.curpage',1);
            $name = trim(I('post.name'));
            $dt_origin_country = I('post.dt_origin_country',0,'int');
            $size = I('post.size',10);
            $ids = I('post.ids');
            $normal_sp = I('post.normal_sp',0); //仅上架未删除商品
            $map = [
                'store_id' => $store_id
                // 'is_second' => 0,
            ];
            if(boolval($normal_sp)){
                $map['onshelf'] = 1;
                $map['status'] = 1;
            }
            if(!empty($cate_id)){
                $map['cate_id'] = $cate_id;
            }
            if(!empty($name)){
                $map['name'] = ['like','%'.$name.'%'];
            }
            if(!empty($dt_origin_country)){
                $map['dt_origin_country'] = $dt_origin_country;
            }
            if(!empty($ids)){
                $map['id'] = ['in',$ids];
            }

            $page = ['curpage'=>$curpage,'size'=>$size];
            $result = apiCall(ProductApi::QUERY,[$map,$page]);
            if($result['status']){
                $list = $result['info']['list'];
                $count = $result['info']['count'];
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                foreach($list as &$val){
                    $val['img_id'] = $PICTURE_REMOTE_URL.$val['img_id'].'&size=120';
                }
                if(is_null($list)){
                    $list = [];
                }
                $this->ajaxReturn(['status'=>true,'info'=>['count'=>$count,'list'=>$list]]);
            }else{
                $this->ajaxReturn(['status'=>false,'info'=>'error']);
            }


        }
    }

    /**
     * 查找商品SKU
     */
    public function querySku(){

        if(IS_POST){

            $pid = I('post.pid');
            $map = ['id'=> $pid];
            //查询商品名称
            $result = apiCall(ProductApi::GET_INFO,[$map]);
            if($result['status']){
                if(is_null($result['info']))$this->ajaxReturn(['status'=>false,'info'=>'该商品不存在']);
            }else{
                $this->ajaxReturn(['status'=>false,'info'=>'error']);
            }
            $product_name = $result['info']['name'];


            $map = [
                'product_id' => $pid
            ];
            $result = apiCall(ProductSkuApi::QUERY_NO_PAGING,[$map]);
            if($result['status']){
                $list = $result['info'];
                if(is_null($list)){
                    $list = [];
                }
                foreach($list as &$val){
                    $val['product_name'] = $product_name;
                }
                $this->ajaxReturn(['status'=>true,'info'=>$list]);
            }else{
                $this->ajaxReturn(['status'=>false,'info'=>'error']);
            }


        }

    }
    /**
     * 保存page 布局
     */
    public function savePage(){
        if(IS_POST){

            $pageInfoJson = htmlspecialchars_decode(I('post.page_info'));
            $pageInfo = json_decode($pageInfoJson,true);

            $result = apiCall(ViewPageApi::SAVE_PAGE_INFO,[$pageInfo]);

            $this->ajaxReturn(['status'=>true,'info'=>'保存成功']);


        }
    }

    /**
     * 获取page 布局
     */
    public function getPage(){
        if(IS_POST){
            $page_name = I('name');
            $page_id = I('id',0);
            $page_notes = I('notes');
           
            if(empty($page_name) && empty($page_notes) && $page_id==0){
                $this->ajaxReturnErr('请求参数错误');
            }

            $where = [
                'name' => $page_name,
                'notes' => $page_notes,
                '_logic' => 'or',
            ];

            $map = [
                'id'=> $page_id,
                '_complex' => $where,
                '_logic' => 'or'
            ];

            $result = apiCall(ViewPageApi::GET_PAGE_INFO,[$map]);
            if($result['status']){
                if(is_null($result['info'])){
                    $this->ajaxReturnErr('页面不存在');
                }
            }else{
                $this->ajaxReturnErr();
            }

            $pageData = [];
            foreach($result['info']['item_data'] as $val){
                $item = json_decode($val['data'],true);
                $item['block_id'] = $val['id'];
                $item['block_name'] = $val['item_name'];
                $item['block_type'] = $val['dt_item_type'];
                $pageData[] = $item;
            }
            $page_info = [
                'page_id' => $result['info']['id'],
                'page_data' => $pageData
            ];
            $this->ajaxReturnSuc($page_info);
        }
    }


    /**
     * 添加分类page
     */
    public function addPageFenlei(){

        if(IS_POST){
            $page_name = I('name');
            $notes = I('notes');
            $map = [
                'name' => $page_name,
                'notes' => $notes
            ];
            $result = apiCall(ViewPageApi::GET_INFO,[$map]);
            if($result['status']){
                if(is_null($result['info'])){
                    $data = [
                        'name' => $page_name,
                        'notes' => $notes
                    ];
                    $result = apiCall(ViewPageApi::ADD,[$data]);
                    if($result['status']){
                        $this->ajaxReturnSuc('添加成功');
                    }else{
                        $this->ajaxReturnErr('添加失败');
                    }
                }else{
                    $this->ajaxReturnErr($page_name.'已存在');
                }
            }else{
                $this->ajaxReturnErr('添加失败');
            }
        }

    }

    /**
     * 获取分类page
     */
    public function getPageFenlei(){

        if(IS_POST){
            $map = [
                'notes' => I('notes')
            ];
            $result = apiCall(ViewPageApi::QUERY_NO_PAGING,[$map]);
            if($result['status']){
                $this->ajaxReturnSuc($result['info']);
            }else{
                $this->ajaxReturnSuc([]);
            }
        }

    }

    /**
     * 删除page
     */
    public function deletePage(){

        if(IS_POST){
            $page_id = I('post.page_id');
            if($page_id==0){
                $this->ajaxReturnErr('参数错误');
            }
            $map = [
                'id' => $page_id
            ];
            $result = apiCall(ViewPageApi::DELETE_PAGE,[$map]);
            if($result['status']){
                $this->ajaxReturnSuc('删除成功');
            }else{
                $this->ajaxReturnErr('删除失败');
            }
        }

    }

    /**
     * 删除block
     */
    public function deleteBlock(){

        if(IS_POST){
            $page_id = I('post.page_id');
            $block_id = I('post.block_id');

            $map = [
                'page_id' => $page_id,
                'id' => $block_id
            ];
            $result = apiCall(ViewPageItemApi::DELETE,[$map]);
            if($result['status']){
                $this->ajaxReturnSuc('删除成功');
            }else{
                $this->ajaxReturnErr('删除失败');
            }
        }

    }


    protected function ajaxReturnErr($info='error'){
        $this->ajaxReturn(['status'=>false,'info'=>$info]);
    }

    protected function ajaxReturnSuc($info='success'){
        $this->ajaxReturn(['status'=>true,'info'=>$info]);
    }
}